package com.seu.clab.farm.controller;

import com.seu.clab.farm.common.CONST;
import com.seu.clab.farm.common.annotation.TokenRequired;
import com.seu.clab.farm.common.annotation.WebResponse;
import com.seu.clab.farm.exception.BizException;
import com.seu.clab.farm.model.dao.entity.Contract;
import com.seu.clab.farm.model.dao.mapper.AccountMapper;
import com.seu.clab.farm.model.dto.post.SuccessDTO;
import com.seu.clab.farm.model.dao.mapper.ContractMapper;
import com.seu.clab.farm.model.dto.post.ContractDTO;
import com.seu.clab.farm.service.ContractService;
import com.seu.clab.farm.util.ConstantUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;

@RestController
@CrossOrigin
@RequestMapping("/contract")
@WebResponse
public class ContractController {

    @Autowired
    HttpServletRequest request;

    @Autowired
    ContractService contractService;

    @Autowired
    private ContractMapper contractMapper;

    @Autowired
    AccountMapper accountMapper;

    @TokenRequired
    @GetMapping
    public Object get(@RequestParam Integer flag) {
        String accountId = (String) request.getAttribute(CONST.ACL_ACCOUNTID);
        return contractService.getContractit(flag, accountId);
    }

    @TokenRequired
    @PostMapping("/add")
    public Object add(@RequestBody ContractDTO contractDTO) {
        String accountId = (String) request.getAttribute(CONST.ACL_ACCOUNTID);
        Contract contract = contractService.addContract(contractDTO, accountId);
        return new ContractDTO(contract);
    }

    @TokenRequired
    @PostMapping("/modify")
    public Object modify(@RequestBody ContractDTO contractDTO) {
        String accountId = (String) request.getAttribute(CONST.ACL_ACCOUNTID);
        Contract contract = contractService.modifyContract(contractDTO, accountId);
        return new ContractDTO(contract);
    }

    @TokenRequired
    @DeleteMapping
    public Object delete(@RequestBody ContractDTO contractDTO) {
        String accountId = (String) request.getAttribute(CONST.ACL_ACCOUNTID);
        Contract contract = contractService.deleteContract(contractDTO, accountId);
        return new ContractDTO(contract);
    }

    @TokenRequired
    @GetMapping("/detail")
    public Object getContractDetail(@RequestParam Long cid) {
        Contract contract = contractService.getContractDetail(cid);
        if (contract != null)
            return new ContractDTO(contract);
        else
            throw new BizException(ConstantUtil.BizExceptionCause.NO_ACCESS_GET_CONTRACT);
    }

    @TokenRequired
    @GetMapping("/list")
    public Object getList(@RequestParam Integer pageIndex) {
        return contractService.getList(pageIndex);
    }

    /**
     * 审核签名(将flag变为2)
     *
     * @param requestBody
     * @return
     */
    @TokenRequired
    @PostMapping("/check")
    public Object checkSignature(@RequestBody SuccessDTO requestBody) {
        Long cid = requestBody.getCid();
        Integer state = requestBody.getState();
        String accountId = (String) request.getAttribute(CONST.ACL_ACCOUNTID);
        if (state.equals(0)) {
            contractService.setFlag(cid, 0, accountId);
        } else {
            contractService.setFlag(cid, 2, accountId);
        }
        return "success";
    }

    /**
     * 生效合同（将flag变为3）
     *
     * @param requestBody
     * @return
     */
    @TokenRequired
    @PostMapping("/success")
    public Object effectContract(@RequestBody SuccessDTO requestBody) {
        Long cid = requestBody.getCid();
        Integer state = requestBody.getState();
        String accountId = (String) request.getAttribute(CONST.ACL_ACCOUNTID);
        if (state.equals(0)) {
            return null;
        }
        contractService.setFlag(cid, 3, accountId);
        return "success";
    }

    /**
     * 保存签名图片，将flag设置为1
     *
     * @param requestBody
     * @return
     */
    @TokenRequired
    @PostMapping("/signature")
    public Object savePdf(@RequestBody Map<String, Object> requestBody) throws IOException {
        String accountId = (String) request.getAttribute(CONST.ACL_ACCOUNTID);
        Long cid = ((Long) requestBody.get("cid")).longValue();
        String signaturePictureUrl = (String) requestBody.get("url");
        String identify = (String) requestBody.get("identify");

        contractService.addSignature(cid, accountId, signaturePictureUrl, identify);
        contractService.setFlag(cid, 1, accountId);
        return "success";
    }

    @TokenRequired
    @GetMapping("/search")
    public Object getSearchContract(@RequestParam String name) {
        Iterator<Contract> iterator = contractMapper.getSearchContract(name).iterator();
        List<Map<String, Object>> list = new LinkedList();
        while (iterator.hasNext()) {
            Contract contract = iterator.next();
            Map<String, Object> map = new HashMap<>();
            String names = contract.getName();
            map.put("cid", contract.getCid());
            map.put("name", names);
            map.put("part_A", contract.getPart_A());
            map.put("part_B", contract.getPart_B());
            map.put("flag", contract.getFlag());
            map.put("money", contract.getMoney());
            map.put("account",accountMapper.getAccountInfo(contract.getPart_A()));
            list.add(map);
        }
        return list;
    }
}
